import md5 from "md5";
const _heart_check = Symbol('_heart_check');
export default class Heart {
    static instanceof = null;
    interval = null
    time = null
    so = null
    constructor() {
        if (!Heart.instanceof) {
            Heart.instanceof = this;
            this.interval = 10;
            this.time = '';
            this.so = null;
        }
        return this.instanceof;
    }
    [_heart_check](fromMemberId) {
        this.time = setInterval(() => {
            var myDate = new Date();
            var msg = {
                head: {
                    po: 2,
                    ms: 1,
                    fromMemberId,
                    clientMsgId: "",
                    action: 'heartbeat',
                    dateTime: myDate.toLocaleTimeString(),
                },
                body: {}
            }
            const md5String = md5(JSON.stringify(msg) + Date.now());
            msg["head"]["clientMsgId"] = fromMemberId + "_" + md5String;
            this.so && this.so.send(JSON.stringify(msg));
        }, this.interval * 1000);
    }
    // 设置so
    init(so) {
        this.so = so;
        return
    }
    start(id, all) {
        if (!this.time) {
            this[_heart_check](id, all)
        }
        return
    }
    stop() {
        if (this.time) {
            clearInterval(this.time);
            this.time = '';
        }
        return
    }
}